package com.api.shopapi.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.*;
import com.api.shopapi.mapper.NursingSchedulingMapper;
import com.api.shopapi.service.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import static com.api.shopapi.controller.BaseController.getWeek;


@Service("NursingSchedulingServiceImplOne")
public class NursingSchedulingServiceImpl implements NursingSchedulingService {

    @Autowired
    private NursingSchedulingMapper nursingSchedulingMapper;

    @Autowired
    @Qualifier("NursingSchedulingDaytimeServiceImplOne")
    private NursingSchedulingDaytimeService nursingSchedulingDaytimeService;

    @Autowired
    @Qualifier("NursingSchedulingNighttimeServiceImplOne")
    private NursingSchedulingNighttimeService nursingSchedulingNighttimeService;

    @Autowired
    @Qualifier("NursingSchedulingMorningServiceImplOne")
    private NursingSchedulingMorningService nursingSchedulingMorningService;

    @Autowired
    @Qualifier("NursingSchedulingNoonServiceImplOne")
    private NursingSchedulingNoonService nursingSchedulingNoonService;

    @Autowired
    @Qualifier("NursingSchedulingNightServiceImplOne")
    private NursingSchedulingNightService nursingSchedulingNightService;

    @Autowired
    @Qualifier("UserServiceImplOne")
    private UserService userService;


    @Autowired
    @Qualifier("NurseServiceImplOne")
    private NurseService nurseService;

    @Autowired
    @Qualifier("NurseAideServiceImplOne")
    private NurseAideService nurseAideService;

    @Autowired
    @Qualifier("NursingSchedulingDaytimeItemServiceImplOne")
    private NursingSchedulingDaytimeItemService nursingSchedulingDaytimeItemService;

    @Autowired
    @Qualifier("NursingSchedulingNighttimeItemServiceImplOne")
    private NursingSchedulingNighttimeItemService nursingSchedulingNighttimeItemService;

    @Autowired
    @Qualifier("NursingSchedulingMorningItemServiceImplOne")
    private NursingSchedulingMorningItemService nursingSchedulingMorningItemService;

    @Autowired
    @Qualifier("NursingSchedulingNoonItemServiceImplOne")
    private NursingSchedulingNoonItemService nursingSchedulingNoonItemService;

    @Autowired
    @Qualifier("NursingSchedulingNightItemServiceImplOne")
    private NursingSchedulingNightItemService nursingSchedulingNightItemService;

    @Autowired
    @Qualifier("StaffServiceImplOne")
    private StaffService staffService;

    @Autowired
    @Qualifier("NursingSchedulingChangeRecordServiceImplOne")
    private NursingSchedulingChangeRecordService nursingSchedulingChangeRecordService;


    @Autowired
    @Qualifier("InstitutionStaffRelServiceImplOne")
    private InstitutionStaffRelService institutionStaffRelService;



    @Override
    @Transactional
    public int insert(NursingScheduling nursingScheduling, JSONArray jsonArray, String nursingSchedulingCode, int type) {
        int count = nursingSchedulingMapper.insert(nursingScheduling);
        addSchedulingPlan(jsonArray,nursingSchedulingCode,type);
        return count;
    }

    private void addSchedulingPlan(JSONArray jsonArray, String nursingSchedulingCode, int type) {
        //type=2:两班倒  3：三班倒
        if(type==2){
            JSONObject dayTimePlan = (JSONObject) jsonArray.get(0);
            JSONObject nightTimePlan = (JSONObject) jsonArray.get(1);

            addDayTime(dayTimePlan,nursingSchedulingCode);
            addNightTime(nightTimePlan,nursingSchedulingCode);

        }
        if(type==3){
            JSONObject morningPlan = (JSONObject) jsonArray.get(0);
            JSONObject noonPlan = (JSONObject) jsonArray.get(1);
            JSONObject nightPlan = (JSONObject) jsonArray.get(2);

            addMorning(morningPlan,nursingSchedulingCode);
            addNoon(noonPlan,nursingSchedulingCode);
            addNight(nightPlan,nursingSchedulingCode);
        }
    }

    //两班倒  白班添加
    private void addDayTime(JSONObject dayTimePlan, String nursingSchedulingCode) {
        String nursingSchedulingDaytimeCode = UUID.randomUUID().toString();
        NursingSchedulingDaytime nursingSchedulingDaytime = new NursingSchedulingDaytime();
        nursingSchedulingDaytime.setNursingSchedulingDaytimeCode(nursingSchedulingDaytimeCode);
        nursingSchedulingDaytime.setNursingSchedulingCode(nursingSchedulingCode);
        nursingSchedulingDaytimeService.insert(nursingSchedulingDaytime);
        JSONArray nurseCodeArr = dayTimePlan.getJSONArray("nurseCodeArr"); //护士code数组
        JSONArray nurseAideCodeArr = dayTimePlan.getJSONArray("nurseAideCodeArr"); //护工code数组
        int count = nurseCodeArr.size()>nurseAideCodeArr.size() ? nurseCodeArr.size() : nurseAideCodeArr.size();
        //循环取护士和护工的值
        for (int i = 0; i < count; i++){
            String nurseCode = "";
            String nurseName = "";
            if(nurseCodeArr.size()>i){
                nurseCode = nurseCodeArr.get(i).toString();
                Nurse nurse = nurseService.getByNurseCode(nurseCode);
                User user = userService.getByUserCode(nurse.getUserCode());
                nurseName = user.getName();
            }
            String nurseAideCode = "";
            String nurseAideName = "";
            if(nurseAideCodeArr.size()>i){
                nurseAideCode = nurseAideCodeArr.get(i).toString();
                NurseAide nurseAide = nurseAideService.getByNurseAideCode(nurseAideCode);
                User user = userService.getByUserCode(nurseAide.getUserCode());
                nurseAideName = user.getName();
            }
            NursingSchedulingDaytimeItem nursingSchedulingDaytimeItem = new NursingSchedulingDaytimeItem();
            nursingSchedulingDaytimeItem.setNursingSchedulingDaytimeItemCode(UUID.randomUUID().toString());
            nursingSchedulingDaytimeItem.setNursingSchedulingDaytimeCode(nursingSchedulingDaytimeCode);
            nursingSchedulingDaytimeItem.setNurseCode(nurseCode);
            nursingSchedulingDaytimeItem.setNurseName(nurseName);
            nursingSchedulingDaytimeItem.setNurseAideCode(nurseAideCode);
            nursingSchedulingDaytimeItem.setNurseAideName(nurseAideName);
            nursingSchedulingDaytimeItemService.insert(nursingSchedulingDaytimeItem);
        }

    }
    //两班倒  晚班添加
    private void addNightTime(JSONObject nightTimePlan, String nursingSchedulingCode) {
        String nursingSchedulingNighttimeCode = UUID.randomUUID().toString();
        NursingSchedulingNighttime nursingSchedulingNighttime = new NursingSchedulingNighttime();
        nursingSchedulingNighttime.setNursingSchedulingNighttimeCode(nursingSchedulingNighttimeCode);
        nursingSchedulingNighttime.setNursingSchedulingCode(nursingSchedulingCode);
        nursingSchedulingNighttimeService.insert(nursingSchedulingNighttime);
        JSONArray nurseCodeArr = nightTimePlan.getJSONArray("nurseCodeArr"); //护士code数组
        JSONArray nurseAideCodeArr = nightTimePlan.getJSONArray("nurseAideCodeArr"); //护工code数组
        int count = nurseCodeArr.size()>nurseAideCodeArr.size() ? nurseCodeArr.size() : nurseAideCodeArr.size();
        //循环取护士和护工的值
        for (int i = 0; i < count; i++){
            String nurseCode = "";
            String nurseName = "";
            if(nurseCodeArr.size()>i){
                nurseCode = nurseCodeArr.get(i).toString();
                Nurse nurse = nurseService.getByNurseCode(nurseCode);
                User user = userService.getByUserCode(nurse.getUserCode());
                nurseName = user.getName();
            }
            String nurseAideCode = "";
            String nurseAideName = "";
            if(nurseAideCodeArr.size()>i){
                nurseAideCode = nurseAideCodeArr.get(i).toString();
                NurseAide nurseAide = nurseAideService.getByNurseAideCode(nurseAideCode);
                User user = userService.getByUserCode(nurseAide.getUserCode());
                nurseAideName = user.getName();
            }
            NursingSchedulingNighttimeItem nursingSchedulingNighttimeItem = new NursingSchedulingNighttimeItem();
            nursingSchedulingNighttimeItem.setNursingSchedulingNighttimeItemCode(UUID.randomUUID().toString());
            nursingSchedulingNighttimeItem.setNursingSchedulingNighttimeCode(nursingSchedulingNighttimeCode);
            nursingSchedulingNighttimeItem.setNurseCode(nurseCode);
            nursingSchedulingNighttimeItem.setNurseName(nurseName);
            nursingSchedulingNighttimeItem.setNurseAideCode(nurseAideCode);
            nursingSchedulingNighttimeItem.setNurseAideName(nurseAideName);
            nursingSchedulingNighttimeItemService.insert(nursingSchedulingNighttimeItem);
        }
    }



    //三班倒  早班添加
    private void addMorning(JSONObject morningPlan, String nursingSchedulingCode) {
        String nursingSchedulingMorningCode = UUID.randomUUID().toString();
        NursingSchedulingMorning nursingSchedulingMorning = new NursingSchedulingMorning();
        nursingSchedulingMorning.setNursingSchedulingMorningCode(nursingSchedulingMorningCode);
        nursingSchedulingMorning.setNursingSchedulingCode(nursingSchedulingCode);
        nursingSchedulingMorningService.insert(nursingSchedulingMorning);
        JSONArray nurseCodeArr = morningPlan.getJSONArray("nurseCodeArr"); //护士code数组
        JSONArray nurseAideCodeArr = morningPlan.getJSONArray("nurseAideCodeArr"); //护工code数组
        int count = nurseCodeArr.size()>nurseAideCodeArr.size() ? nurseCodeArr.size() : nurseAideCodeArr.size();
        //循环取护士和护工的值
        for (int i = 0; i < count; i++){
            String nurseCode = "";
            String nurseName = "";
            if(nurseCodeArr.size()>i){
                nurseCode = nurseCodeArr.get(i).toString();
                Nurse nurse = nurseService.getByNurseCode(nurseCode);
                User user = userService.getByUserCode(nurse.getUserCode());
                nurseName = user.getName();
            }
            String nurseAideCode = "";
            String nurseAideName = "";
            if(nurseAideCodeArr.size()>i){
                nurseAideCode = nurseAideCodeArr.get(i).toString();
                NurseAide nurseAide = nurseAideService.getByNurseAideCode(nurseAideCode);
                User user = userService.getByUserCode(nurseAide.getUserCode());
                nurseAideName = user.getName();
            }
            NursingSchedulingMorningItem nursingSchedulingMorningItem = new NursingSchedulingMorningItem();
            nursingSchedulingMorningItem.setNursingSchedulingMorningItemCode(UUID.randomUUID().toString());
            nursingSchedulingMorningItem.setNursingSchedulingMorningCode(nursingSchedulingMorningCode);
            nursingSchedulingMorningItem.setNurseCode(nurseCode);
            nursingSchedulingMorningItem.setNurseName(nurseName);
            nursingSchedulingMorningItem.setNurseAideCode(nurseAideCode);
            nursingSchedulingMorningItem.setNurseAideName(nurseAideName);
            nursingSchedulingMorningItemService.insert(nursingSchedulingMorningItem);
        }

    }

    //三班倒  中班添加
    private void addNoon(JSONObject noonPlan, String nursingSchedulingCode) {
        String nursingSchedulingNoonCode = UUID.randomUUID().toString();
        NursingSchedulingNoon nursingSchedulingNoon = new NursingSchedulingNoon();
        nursingSchedulingNoon.setNursingSchedulingNoonCode(nursingSchedulingNoonCode);
        nursingSchedulingNoon.setNursingSchedulingCode(nursingSchedulingCode);
        nursingSchedulingNoonService.insert(nursingSchedulingNoon);
        JSONArray nurseCodeArr = noonPlan.getJSONArray("nurseCodeArr"); //护士code数组
        JSONArray nurseAideCodeArr = noonPlan.getJSONArray("nurseAideCodeArr"); //护工code数组
        int count = nurseCodeArr.size()>nurseAideCodeArr.size() ? nurseCodeArr.size() : nurseAideCodeArr.size();
        //循环取护士和护工的值
        for (int i = 0; i < count; i++){
            String nurseCode = "";
            String nurseName = "";
            if(nurseCodeArr.size()>i){
                nurseCode = nurseCodeArr.get(i).toString();
                Nurse nurse = nurseService.getByNurseCode(nurseCode);
                User user = userService.getByUserCode(nurse.getUserCode());
                nurseName = user.getName();
            }
            String nurseAideCode = "";
            String nurseAideName = "";
            if(nurseAideCodeArr.size()>i){
                nurseAideCode = nurseAideCodeArr.get(i).toString();
                NurseAide nurseAide = nurseAideService.getByNurseAideCode(nurseAideCode);
                User user = userService.getByUserCode(nurseAide.getUserCode());
                nurseAideName = user.getName();
            }
            NursingSchedulingNoonItem nursingSchedulingNoonItem = new NursingSchedulingNoonItem();
            nursingSchedulingNoonItem.setNursingSchedulingNoonItemCode(UUID.randomUUID().toString());
            nursingSchedulingNoonItem.setNursingSchedulingNoonCode(nursingSchedulingNoonCode);
            nursingSchedulingNoonItem.setNurseCode(nurseCode);
            nursingSchedulingNoonItem.setNurseName(nurseName);
            nursingSchedulingNoonItem.setNurseAideCode(nurseAideCode);
            nursingSchedulingNoonItem.setNurseAideName(nurseAideName);
            nursingSchedulingNoonItemService.insert(nursingSchedulingNoonItem);
        }

    }

    //三班倒  晚班添加
    private void addNight(JSONObject nightPlan, String nursingSchedulingCode) {
        String nursingSchedulingNightCode = UUID.randomUUID().toString();
        NursingSchedulingNight nursingSchedulingNight = new NursingSchedulingNight();
        nursingSchedulingNight.setNursingSchedulingNightCode(nursingSchedulingNightCode);
        nursingSchedulingNight.setNursingSchedulingCode(nursingSchedulingCode);
        nursingSchedulingNightService.insert(nursingSchedulingNight);
        JSONArray nurseCodeArr = nightPlan.getJSONArray("nurseCodeArr"); //护士code数组
        JSONArray nurseAideCodeArr = nightPlan.getJSONArray("nurseAideCodeArr"); //护工code数组
        int count = nurseCodeArr.size()>nurseAideCodeArr.size() ? nurseCodeArr.size() : nurseAideCodeArr.size();
        //循环取护士和护工的值
        for (int i = 0; i < count; i++){
            String nurseCode = "";
            String nurseName = "";
            if(nurseCodeArr.size()>i){
                nurseCode = nurseCodeArr.get(i).toString();
                Nurse nurse = nurseService.getByNurseCode(nurseCode);
                User user = userService.getByUserCode(nurse.getUserCode());
                nurseName = user.getName();
            }
            String nurseAideCode = "";
            String nurseAideName = "";
            if(nurseAideCodeArr.size()>i){
                nurseAideCode = nurseAideCodeArr.get(i).toString();
                NurseAide nurseAide = nurseAideService.getByNurseAideCode(nurseAideCode);
                User user = userService.getByUserCode(nurseAide.getUserCode());
                nurseAideName = user.getName();
            }
            NursingSchedulingNightItem nursingSchedulingNightItem = new NursingSchedulingNightItem();
            nursingSchedulingNightItem.setNursingSchedulingNightItemCode(UUID.randomUUID().toString());
            nursingSchedulingNightItem.setNursingSchedulingNightCode(nursingSchedulingNightCode);
            nursingSchedulingNightItem.setNurseCode(nurseCode);
            nursingSchedulingNightItem.setNurseName(nurseName);
            nursingSchedulingNightItem.setNurseAideCode(nurseAideCode);
            nursingSchedulingNightItem.setNurseAideName(nurseAideName);
            nursingSchedulingNightItemService.insert(nursingSchedulingNightItem);
        }
    }



    @Override
    @Transactional
    public int updateByNursingSchedulingCode(NursingScheduling nursingScheduling,JSONArray jsonArray, int type,String remarks) {
        String nursingSchedulingCode = nursingScheduling.getNursingSchedulingCode();
        //先删除数据
        NursingSchedulingDaytime nursingSchedulingDaytime = nursingSchedulingDaytimeService.getByNursingSchedulingCode(nursingSchedulingCode);
        if(nursingSchedulingDaytime!=null){
            nursingSchedulingDaytimeItemService.deleteByNurseSchedulingDaytimeCode(nursingSchedulingDaytime.getNursingSchedulingDaytimeCode());
            nursingSchedulingDaytimeService.deleteByNursingSchedulingDaytimeCode(nursingSchedulingDaytime.getNursingSchedulingDaytimeCode());
        }


        NursingSchedulingNighttime nursingSchedulingNighttime = nursingSchedulingNighttimeService.getByNursingSchedulingCode(nursingSchedulingCode);
        if(nursingSchedulingNighttime!=null){
            nursingSchedulingNighttimeItemService.deleteByNurseSchedulingNighttimeCode(nursingSchedulingNighttime.getNursingSchedulingNighttimeCode());
            nursingSchedulingNighttimeService.deleteByNursingSchedulingNighttimeCode(nursingSchedulingNighttime.getNursingSchedulingNighttimeCode());
        }


        NursingSchedulingMorning nursingSchedulingMorning = nursingSchedulingMorningService.getByNursingSchedulingCode(nursingSchedulingCode);
        if(nursingSchedulingMorning!=null){
            nursingSchedulingMorningItemService.deleteByNurseSchedulingMorningCode(nursingSchedulingMorning.getNursingSchedulingMorningCode());
            nursingSchedulingMorningService.deleteByNursingSchedulingMorningCode(nursingSchedulingMorning.getNursingSchedulingMorningCode());
        }


        NursingSchedulingNoon nursingSchedulingNoon = nursingSchedulingNoonService.getByNursingSchedulingCode(nursingSchedulingCode);
        if(nursingSchedulingNoon!=null){
            nursingSchedulingNoonItemService.deleteByNurseSchedulingNoonCode(nursingSchedulingNoon.getNursingSchedulingNoonCode());
            nursingSchedulingNoonService.deleteByNursingSchedulingNoonCode(nursingSchedulingNoon.getNursingSchedulingNoonCode());
        }



        NursingSchedulingNight nursingSchedulingNight = nursingSchedulingNightService.getByNursingSchedulingCode(nursingSchedulingCode);
        if(nursingSchedulingNight!=null){
            nursingSchedulingNightItemService.deleteByNurseSchedulingNightCode(nursingSchedulingNight.getNursingSchedulingNightCode());
            nursingSchedulingNightService.deleteByNursingSchedulingNightCode(nursingSchedulingNight.getNursingSchedulingNightCode());
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String time1 = sdf.format(nursingScheduling.getTime());
        String time2 = sdf.format(new Date());
        if(time1.equals(time2)){
            NursingSchedulingChangeRecord nursingSchedulingChangeRecord = new NursingSchedulingChangeRecord();
            nursingSchedulingChangeRecord.setNursingSchedulingChangeRecordCode(UUID.randomUUID().toString());
            nursingSchedulingChangeRecord.setNursingSchedulingCode(nursingSchedulingCode);
            nursingSchedulingChangeRecord.setCreateTime(new Date());
            nursingSchedulingChangeRecord.setStaffCode(nursingScheduling.getStaffCode());
            nursingSchedulingChangeRecord.setInstitutionCode(nursingScheduling.getInstitutionCode());
            nursingSchedulingChangeRecord.setRemarks(remarks);
            nursingSchedulingChangeRecordService.insert(nursingSchedulingChangeRecord);
        }

        addSchedulingPlan(jsonArray,nursingSchedulingCode,type);
        return nursingSchedulingMapper.updateByNursingSchedulingCode(nursingScheduling);
    }

    @Override
    public int deleteByNursingSchedulingCode(String nursingSchedulingCode) {
        return nursingSchedulingMapper.deleteByNursingSchedulingCode(nursingSchedulingCode);
    }

    @Override
    public NursingScheduling getByNursingSchedulingCode(String nursingSchedulingCode) {
        return nursingSchedulingMapper.getByNursingSchedulingCode(nursingSchedulingCode);
    }

    @Override
    public NursingScheduling getByTime(Date time,String institutionCode) {
        return nursingSchedulingMapper.getByTime(time,institutionCode);
    }

    @Override
    public NursingScheduling getByNowTime(String institutionCode) {
        return nursingSchedulingMapper.getByNowTime(institutionCode);
    }

    @Override
    public PageInfo<NursingScheduling> getPageList(int pageIndex, int pageSize, String institutionCode, String startTime, String endTime) {
        PageHelper.startPage(pageIndex, pageSize);
        List<NursingScheduling> nursingSchedulingList = nursingSchedulingMapper.getListByTime(institutionCode,startTime,endTime);
        PageInfo<NursingScheduling> pageInfo = new PageInfo<>(nursingSchedulingList);
        return pageInfo;
    }

    @Override
    @Transactional
    public List<String> insert1(JSONObject jsonObject) {
        String staffCode = jsonObject.getString("staffCode");
        int type = jsonObject.getInteger("type");
        JSONArray timeArr = jsonObject.getJSONArray("timeArr");
        JSONArray jOSchedulingPlan = jsonObject.getJSONArray("schedulingPlan");
        List<String> strList = new ArrayList<>();

        InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
        String institutionCode = institutionStaffRel.getInstitutionCode();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        for (Object i :timeArr){
            String time = (String) i;
            Date times = null;
            try {
                times = sdf.parse(time);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            NursingScheduling nursingScheduling = nursingSchedulingMapper.getByTime(times,institutionCode);
            if(nursingScheduling!=null){
                strList.add(time+"的排班已存在");
            }else {
                nursingScheduling = new NursingScheduling();
                String nursingSchedulingCode = UUID.randomUUID().toString();
                nursingScheduling.setNursingSchedulingCode(nursingSchedulingCode);
                nursingScheduling.setDay(getWeek(time));
                nursingScheduling.setTime(times);
                nursingScheduling.setCreateTime(new Date());
                nursingScheduling.setStatus(0);
                nursingScheduling.setType(type);
                nursingScheduling.setStaffCode(staffCode);
                nursingScheduling.setInstitutionCode(institutionCode);
                nursingSchedulingMapper.insert(nursingScheduling);
                addSchedulingPlan(jOSchedulingPlan,nursingSchedulingCode,type);
            }
        }
        return strList;
    }

    @Override
    public
    PageInfo<NursingScheduling> pageByGovCode(int pageIndex, int pageSize, String governmentCode,
                                              String startTime, String endTime){
        PageHelper.startPage(pageIndex, pageSize);
        List<NursingScheduling> nursingSchedulingList = nursingSchedulingMapper.listByGovCode(governmentCode,startTime,endTime);
        PageInfo<NursingScheduling> pageInfo = new PageInfo<>(nursingSchedulingList);
        return pageInfo;
    }



}
